<?php

/**
 * 用户模型
 */
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class User_model extends MY_Model
{

    public $_table = array(
        'user' => 'user',
        'group' => 'group',
    );
    public $_fields = array(
        'user' => array(
            'id' => 'id',
            'name' => 'name',
            'password' => 'password',
            'email' => 'email',
            'regtime' => 'regtime',
            'groupid' => 'groupid',
        ),
        'group' => array(
            'id' => 'groupid',
            'name' => 'groupname',
        ),
    );

    /**
     * 插入新用户记录
     * @param array $data 用户数据
     * @return int 用户ID
     */
    public function add( $data )
    {
        $insertData = array(
            'name' => $data['username'],
            'password' => $data['password'],
            'email' => $data['email'],
            'regtime' => isset( $data['regtime'] ) ? $data['regtime'] : time(),
            'groupid' => isset( $data['groupid'] ) ? intval( $data['groupid'] ) : 1,
        );
        $this->lock( $this->_table['user'], true );
        $this->db->insert( $this->_table['user'], $insertData );
        $this->unlock();
        return $this->insert_id();
    }

    /**
     * 验证用户登录
     * @param string $username 用户名
     * @param string $password 密码
     * @return array 用户数据
     */
    public function valid( $username, $password )
    {
        return $this->db->select( 'u.id,u.name,u.email,u.regtime,u.groupid,g.id as groupid,g.name as groupname' )
                        ->from( "{$this->_table['user']} as u" )
                        ->join( "{$this->_table['group']} as g", 'u.groupid=g.id' )
                        ->where( 'u.name', $username )
                        ->where( 'u.password', $password )
                        ->get()
                        ->row_array();
    }

    /**
     * 根据用户名获取用户数据
     * @param string $userName 用户名
     */
    public function getByUserName( $userName )
    {
        return $this->db->select( 'u.id,u.name,u.email,u.regtime,u.groupid,g.id as groupid,g.name as groupname' )
                        ->from( "{$this->_table['user']} as u" )
                        ->join( "{$this->_table['group']} as g", 'u.groupid=g.id' )
                        ->where( 'u.name', $userName )
                        ->get()
                        ->row_array();
    }

    public function getByUserId( $userId )
    {
        return $this->db->select( 'u.id,u.name,u.email,u.regtime,u.groupid,g.id as groupid,g.name as groupname' )
                        ->from( "{$this->_table['user']} as u" )
                        ->join( "{$this->_table['group']} as g", 'u.groupid=g.id' )
                        ->where( 'u.id', $userId )
                        ->get()
                        ->row_array();
    }
 
    public function updateUserEmailByUserId( $userId, $email )
    {
        $this->db->where('id', $userId)->update($this->_table['user'], array('email'=>$email));
    }
    
    public function updateUserPasswordByUserId( $userId, $password )
    {
        $this->db->where('id', $userId)->update($this->_table['user'], array('password'=>$password));
    }
    
}

?>
